package com.winexpress.session.product;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.winexpress.entity.Category;
import com.winexpress.entity.Wine;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class ProductSessionBean {

    @PersistenceContext
    private EntityManager em;

    public boolean addCategory(Category c) {
        em.persist(c);
        em.flush();
        return true;
    }

    public Long addWine(Wine newWine) {
        em.persist(newWine);
        em.flush();
        return newWine.getId();
    }

    public boolean editWine(Wine editedWine) {
        em.merge(editedWine);
        em.flush();
        return true;
    }

    public boolean deleteCategory(Long id) {
        Category category = em.find(Category.class, id);
        if (category.getWine().isEmpty()) {
            em.remove(category);
            return true;
        } else {
            return false;
        }
    }

    public boolean editCategory(Category c) {
        em.merge(c);
        em.flush();
        return true;
    }

    public List<Category> listCategory() {
        String strQuery = "SELECT c FROM Category c";
        Query query = em.createQuery(strQuery);
        List<Category> list = (List<Category>) query.getResultList();

        if (!list.isEmpty()) {
            return list;
        } else {
            return null;
        }
    }

    public List<Wine> listWines() {
        String strQuery = "SELECT w FROM Wine w";
        Query query = em.createQuery(strQuery);
        List<Wine> list = (List<Wine>) query.getResultList();
        if (!list.isEmpty()) {
            return list;
        } else {
            return null;
        }
    }

    public List<Wine> listWinesforShop() {
        String strQuery = "SELECT w FROM Wine w WHERE w.isActive = :active";
        Query query = em.createQuery(strQuery);
        query.setParameter("active", true);
        List<Wine> list = (List<Wine>) query.getResultList();

        if (!list.isEmpty()) {
            return list;
        } else {
            return null;
        }
    }

    public boolean deleteWine(Long id) {
        Wine wine = em.find(Wine.class, id);
        wine.setIsActive(false);
        em.persist(wine);
        em.flush();
        return true;
    }

    private boolean deleteWineFromCategory(long wineID, long categoryID) {
        Category c = this.getCategory(categoryID);
        List<Wine> list = c.getWine();

        Iterator it = list.iterator();
        while (it.hasNext()) {
            Wine w = (Wine) it.next();
            if (w.getId() == wineID) {
                it.remove();
            }
        }
        return true;
    }

    public boolean relistWine(Long id) {
        Wine wine = em.find(Wine.class, id);
        wine.setIsActive(true);
        em.persist(wine);
        return true;
    }

    public Wine getWine(Long id) {
        return em.find(Wine.class, id);
    }

    public Category getCategoryID(Long id) {
        return em.find(Category.class, id);
    }

    public Category getCategory(Long id) {
        return em.find(Category.class, id);
    }

    public List<Category> getAllCategories() {
        Query q = em.createQuery("SELECT * FROM category");
        return q.getResultList();
    }

    public boolean hasName(String name) {
        Boolean flag = false;
        List<Category> list = listCategory();
        if (list == null) {
            return flag;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().toUpperCase().equals(name.toUpperCase())) {
                flag = true;
                return flag;
            }
        }
        return flag;
    }

    public Category findCategory(String wineID) {
        Category c = null;
        try {
            String url = "jdbc:mysql://localhost:3306/winexpress?zeroDateTimeBehavior=convertToNull";
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url, "root", "root");
            } catch (SQLException ex) {
                Logger.getLogger(ProductSessionBean.class.getName()).log(Level.SEVERE, null, ex);
            }

            Statement stmt = conn.createStatement();
            ResultSet rs;

            String st = "SELECT Category_CATEGORYID FROM Category_wine WHERE wine_ID = " + wineID;
            rs = stmt.executeQuery(st);
            while (rs.next()) {
                String categoryID = rs.getString("Category_CATEGORYID");
                c = em.find(Category.class, new Long(categoryID));
            }
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(ProductSessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return c;
    }

    public List<Wine> returnCartItems(String cartSession) {
        List<Wine> items = new ArrayList<Wine>();
        String[] array = cartSession.split("\\;");
        for (int i = 0; i < array.length; i = i + 2) {
            Long id = Long.parseLong(array[i]);
            Wine w = em.find(Wine.class, id);
            items.add(w);
        }
        return items;
    }
}
